package com.xiaoy.core.base.security.permissions.service;

import java.util.List;

import com.xiaoy.core.base.security.exception.PasswordErrorException;
import com.xiaoy.core.base.security.org.entity.Org;
import com.xiaoy.core.base.security.org.service.OrgService;
import com.xiaoy.core.base.security.permissions.entity.SessionFacade;
import com.xiaoy.core.base.security.role.entity.Role;
import com.xiaoy.core.base.security.user.entity.User;
import com.xiaoy.core.base.security.user.service.UserService;

public class LoginService {
	
	private UserService userService;
	private OrgService orgService;
	
	public SessionFacade login (String username,String password) throws Exception{
		
		
		// 验证用户
		User user = userService.getByUsername(username);
		// 验证用户密码
		if (!password.equals(user.getPassword())) {
			throw new PasswordErrorException(username+"'s password is error");
		}
		
		// 处理权限 -start
		List<Role> roles = userService.getRoleByUserId(user.getId());
		
		// 得到组织机构
		List<Org> orgs = orgService.getByUser(user.getId());
		// 目前设置一个用户只能在一个部门下
		Org current = null;
		if (orgs.size()>0) {
			current = orgs.get(0);
		}
		
		// 得到所有能继承角色的父机构
		List<Role>org_roles = orgService.getParentOrgMergeRole(current.getId());
		
		// 获得角色
		roles.addAll(org_roles);
		// 处理角色
		// 获得能够操作的菜单和按钮
		String menu_sql = "select * from ";
		String button_sql = "";
		
		SessionFacade sf = SessionFacade.get();
		
//		List<Map<String>>
	
		// 组织机构继承下来的角色
		return sf;
	}

}
